package com.apps.apicontroller;

import com.apps.common.model.po.ApiResult;
import com.apps.goods.model.po.KupaGoods;
import com.apps.goods.model.po.KupaGoodsComment;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.helper.UserConextHelper;
import com.apps.member.service.IKupaMemberService;
import com.apps.module.pay.model.vo.KupaPayDataVo;
import com.apps.order.model.po.KupaVipGoodsOrder;
import com.apps.order.service.IKupaVipGoodsOrderService;
import com.apps.sannotation.constraints.Mobile;
import com.apps.saspect.ValidMethod;
import com.apps.sexctption.NullException;
import com.apps.thirdparty.pay.wxpay.model.po.KupaWxpayReturn;
import com.apps.thirdparty.pay.wxpay.service.IKupaWxPayService;
import com.apps.thirdparty.sms.service.IKupaSmsService;
import com.apps.vip.model.po.KupaVipGoods;
import com.apps.vip.service.IKupaVipGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(description = "用户操作接口")
@RestController
@RequestMapping("/api/member")
public class MemberApiController {

    @Autowired
    private IKupaMemberService memberService;

    @Autowired
    private IKupaWxPayService wxPayService;

    @Autowired
    private IKupaVipGoodsService vipGoodsService;

    @Autowired
    private IKupaVipGoodsOrderService vipGoodsOrderService;

    @Autowired
    private IKupaGoodsService goodsService;

    @Autowired
    private IKupaSmsService smsPlugs;

    @ApiOperation(value = "修改昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickname", value = "新昵称", required = true,dataType = "string",paramType = "query")
    })
    @PostMapping("/modifyNickName")
    ApiResult<String> modifyNickName(@RequestParam(value = "nickname") String nickname)
    {
        ApiResult<String> result = new ApiResult<>();
        memberService.modifyNickName(UserConextHelper.getCurrentMember().getId(),nickname);
        return result.success("修改成功");
    }

    @ApiOperation(value = "绑定邮箱账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱账号", required = true,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "validcode", value = "邮箱验证码", required = true,dataType = "string",paramType = "query")
    })
    @PostMapping("/bindEmail")
    ApiResult<String> bindEmail(@RequestParam(value = "email") String email,
                                     @RequestParam(value = "validcode") String validcode)
    {
        ApiResult<String> result = new ApiResult<>();
        memberService.bindEmail(UserConextHelper.getCurrentMember().getId(),email,validcode);
        return result.success("绑定成功");
    }

    @ApiOperation(value = "绑定手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "validcode", value = "邮箱验证码", required = true,dataType = "string",paramType = "query")
    })
    @PostMapping("/bindMobile")
    ApiResult<String> bindMobile(@RequestParam(value = "mobile") String mobile,
                                @RequestParam(value = "validcode") String validcode)
    {
        ApiResult<String> result = new ApiResult<>();
        memberService.bindMobile(UserConextHelper.getCurrentMember().getId(),mobile,validcode);
        return result.success("绑定成功");
    }

    @ApiOperation(value = "收藏商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id", required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/collectGoods")
    ApiResult<String> collectGoods(@RequestParam(value = "goods_id") Integer goods_id)
    {
        ApiResult<String> result = new ApiResult<>();
        memberService.collectGoods(UserConextHelper.getCurrentMember().getId(),goods_id);
        return result.success("收藏成功");
    }

    @ApiOperation(value = "取消收藏商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id", required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/cancelCollectGoods")
    ApiResult<String> cancelCollectGoods(@RequestParam(value = "goods_id") Integer goods_id)
    {
        ApiResult<String> result = new ApiResult<>();
        memberService.cancelCollectGoods(UserConextHelper.getCurrentMember().getId(),goods_id);
        return result.success("取消收藏成功");
    }

    @ApiOperation(value = "评论商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id", required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "context", value = "评论内容", required = true,dataType = "string",paramType = "query")
    })
    @PostMapping("/commentGoods")
    ApiResult<List<KupaGoodsComment>> commentGoods(@RequestParam(value = "goods_id") Integer goods_id,
                                                   @RequestParam(value = "context") String context)
    {
        ApiResult<List<KupaGoodsComment>> result = new ApiResult<>();
        return result.success(memberService.commentGoods(UserConextHelper.getCurrentMember().getId(),goods_id,context));
    }


    @ApiOperation(value = "下载商品",notes = "请求成功返回资源下载链接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_id", value = "商品id", required = true,dataType = "int",paramType = "query")
    })
    @PostMapping("/downloadGoods")
    ApiResult<String> downloadGoods(@RequestParam(value = "goods_id") Integer goods_id)
    {
        ApiResult<String> result = new ApiResult<>();
        KupaGoods goods=memberService.downloadGoods(UserConextHelper.getCurrentMember().getId(),goods_id);
        if(null == goods)
            throw new NullException("您不能下载");
        return result.success(goods.getGoods_download());
    }

    @ApiOperation(value = "通用发送短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "接收验证码的手机号", required = true, dataType = "string",paramType = "query")
    })
    @ValidMethod
    @PostMapping("/SendSmsCode")
    public ApiResult<String> sendSmsCode(@Mobile @RequestParam(value = "mobile") String mobile)
    {
        ApiResult<String> result = new ApiResult<>();
        smsPlugs.sendValidCodeSms(mobile);
        return result.success("发送成功");
    }


}
